package com.star.service.impl;

import com.star.context.BaseContext;
import com.star.dto.ShoppingCartDTO;
import com.star.entity.Dish;
import com.star.entity.Setmeal;
import com.star.entity.ShoppingCart;
import com.star.mapper.DishMapper;
import com.star.mapper.SetmealMapper;
import com.star.mapper.ShoppingCartMapper;
import com.star.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 根据商家id查看用户购物车列表
     * @param businessId
     * @return
     */
    public List<ShoppingCart> businessList(Long businessId) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).businessId(businessId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断当前用户购物车商品是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(shoppingCartList != null && !shoppingCartList.isEmpty()){
            // 存在数量+1
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else {
            // 不存在，插入
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if(dishId != null){
                // 菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                // 套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 插入
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }




    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        // 根据shoppingCart查询购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = shoppingCartList.get(0);
        // 判断数量是否大于1
        if(cart.getNumber() > 1){
            // 大于1
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        }else {
            shoppingCartMapper.delete(cart.getId());
        }
    }

    /**
     * 清空购物车
     */
    public void clear() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clear(userId);
    }
}
